package org.dei.perla.aggregates;

import java.util.Random;

import org.dei.perla.parser.expressions.ConstantID;
import org.dei.perla.parser.expressions.ConstantInteger;
import org.dei.perla.parser.expressions.ConstantTimestamp;
import org.dei.perla.sys.query.llqruntime.datastructures.Buffer;
import org.dei.perla.sys.query.llqruntime.datastructures.QueryRecordStructure;
import org.dei.perla.sys.query.llqruntime.datastructures.Record;

public class ThreadWriteBuffer implements Runnable {
	private Buffer pBuffer;

	public ThreadWriteBuffer(Buffer pBuffer) {
		this.pBuffer = pBuffer;
	}

	public void run() { // Generating a Buffer sample with 10 records.
		/*
		 * Defining the field structure
		 */
		QueryRecordStructure parQueryRecordStructure = new QueryRecordStructure();
		parQueryRecordStructure.addFieldStructure("ID", ConstantID.class);
		parQueryRecordStructure
				.addFieldStructure("TS", ConstantTimestamp.class);
		parQueryRecordStructure
				.addFieldStructure("TEMP", ConstantInteger.class);
		Random random = new Random();

		for (int i = 0; i < 10; i++) {
			Record record = StreamVariance
					.randomRecord(parQueryRecordStructure);

			pBuffer.addRecord(record);

			try {
				Thread.sleep(random.nextInt(500));
			} catch (InterruptedException e) {
			}
		}
	}
}
